package com.hxalex.simulationhandle.db;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.hxalex.simulationhandle.ui.home.model.TaskBean;

import com.hxalex.simulationhandle.ui.home.model.WebSiteBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "WEB_SITE_BEAN".
*/
public class WebSiteBeanDao extends AbstractDao<WebSiteBean, Long> {

    public static final String TABLENAME = "WEB_SITE_BEAN";

    /**
     * Properties of entity WebSiteBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property WebSiteId = new Property(0, Long.class, "webSiteId", true, "_id");
        public final static Property WebSiteString = new Property(1, String.class, "WebSiteString", false, "WEB_SITE_STRING");
        public final static Property TaskId = new Property(2, Long.class, "taskId", false, "TASK_ID");
    }

    private DaoSession daoSession;

    private Query<WebSiteBean> taskBean_WebSiteBeanListQuery;

    public WebSiteBeanDao(DaoConfig config) {
        super(config);
    }
    
    public WebSiteBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"WEB_SITE_BEAN\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: webSiteId
                "\"WEB_SITE_STRING\" TEXT," + // 1: WebSiteString
                "\"TASK_ID\" INTEGER);"); // 2: taskId
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"WEB_SITE_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, WebSiteBean entity) {
        stmt.clearBindings();
 
        Long webSiteId = entity.getWebSiteId();
        if (webSiteId != null) {
            stmt.bindLong(1, webSiteId);
        }
 
        String WebSiteString = entity.getWebSiteString();
        if (WebSiteString != null) {
            stmt.bindString(2, WebSiteString);
        }
 
        Long taskId = entity.getTaskId();
        if (taskId != null) {
            stmt.bindLong(3, taskId);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, WebSiteBean entity) {
        stmt.clearBindings();
 
        Long webSiteId = entity.getWebSiteId();
        if (webSiteId != null) {
            stmt.bindLong(1, webSiteId);
        }
 
        String WebSiteString = entity.getWebSiteString();
        if (WebSiteString != null) {
            stmt.bindString(2, WebSiteString);
        }
 
        Long taskId = entity.getTaskId();
        if (taskId != null) {
            stmt.bindLong(3, taskId);
        }
    }

    @Override
    protected final void attachEntity(WebSiteBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public WebSiteBean readEntity(Cursor cursor, int offset) {
        WebSiteBean entity = new WebSiteBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // webSiteId
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // WebSiteString
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2) // taskId
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, WebSiteBean entity, int offset) {
        entity.setWebSiteId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setWebSiteString(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setTaskId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(WebSiteBean entity, long rowId) {
        entity.setWebSiteId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(WebSiteBean entity) {
        if(entity != null) {
            return entity.getWebSiteId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(WebSiteBean entity) {
        return entity.getWebSiteId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "webSiteBeanList" to-many relationship of TaskBean. */
    public List<WebSiteBean> _queryTaskBean_WebSiteBeanList(Long webSiteId) {
        synchronized (this) {
            if (taskBean_WebSiteBeanListQuery == null) {
                QueryBuilder<WebSiteBean> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.WebSiteId.eq(null));
                taskBean_WebSiteBeanListQuery = queryBuilder.build();
            }
        }
        Query<WebSiteBean> query = taskBean_WebSiteBeanListQuery.forCurrentThread();
        query.setParameter(0, webSiteId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getTaskBeanDao().getAllColumns());
            builder.append(" FROM WEB_SITE_BEAN T");
            builder.append(" LEFT JOIN TASK_BEAN T0 ON T.\"TASK_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected WebSiteBean loadCurrentDeep(Cursor cursor, boolean lock) {
        WebSiteBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        TaskBean taskBean = loadCurrentOther(daoSession.getTaskBeanDao(), cursor, offset);
        entity.setTaskBean(taskBean);

        return entity;    
    }

    public WebSiteBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<WebSiteBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<WebSiteBean> list = new ArrayList<WebSiteBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<WebSiteBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<WebSiteBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
